﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using Services;



namespace WebRole1
{
	public partial class NewTopic : System.Web.UI.Page
	{
		private void Page_Init(object sender, System.EventArgs e)
		{
			this.EnableViewState = false;
		}



		public System.Drawing.Color Color1
		{
			get
			{
				return System.Drawing.Color.White;
			}
		}

		public System.Drawing.Color Color2
		{
			get
			{
				return System.Drawing.Color.FromArgb(238, 238, 238);
			}
		}


		public System.Drawing.Color ColorQuote
		{
			get
			{
				return System.Drawing.Color.FromArgb(180, 180, 180);
			}
		}


		//private Int16 backcolorTypeQuote = 99;




		protected void Page_Load(object sender, EventArgs e)
		{
			btnSubmit.Attributes.Add("onclick", "this.disabled=true;" + ClientScript.GetPostBackEventReference(btnSubmit, null).ToString());


			HttpCookie aCookie = Request.Cookies["ForumSettings"];
			if (aCookie != null)
			{
				if (aCookie["Username"] != null)
				{
					lblUsername.Visible = false;
					txtUsername.Visible = false;
				}
			}



			Int64 topicID = 0;
			Int64 replyToID = 0;

			if (HttpContext.Current.Request.QueryString["Topic"] != null)
			{
				topicID = Int64.Parse(HttpContext.Current.Request.QueryString["Topic"].ToString());
			}


			if (HttpContext.Current.Request.QueryString["ReplyTo"] != null)
			{
				replyToID = Int64.Parse(HttpContext.Current.Request.QueryString["ReplyTo"].ToString());
			}


			if (topicID != 0)
			{
				TopicService topicServ = new TopicService();
				txtTitle.Text = topicServ.getTopicTitle(topicID);

				PostingService postingServ = new PostingService();
				DataSet dst = postingServ.getPost(replyToID);

				txtMsg.Text = "[quote]Op " + dst.Tables[0].Rows[0]["Timestamp"].ToString() + " schreef " + dst.Tables[0].Rows[0]["Username"].ToString() + ":\n" + dst.Tables[0].Rows[0]["Content"].ToString() + "[/quote]\n";
			}

			if (!IsPostBack)
			{
			}

		}




		protected void btnSubmit_Click(object sender, EventArgs e)
		{
			Int16 subforumID = 0;
			Int64 topicID = 0;

			if (HttpContext.Current.Request.QueryString["subforum"] != null)
			{
				subforumID = Int16.Parse(HttpContext.Current.Request.QueryString["subforum"].ToString());
			}

			string message = Server.HtmlEncode(txtMsg.Text);


			string username = "";
			string userID = "";
			HttpCookie aCookie = Request.Cookies["ForumSettings"];
			if (aCookie != null)
			{
				if (aCookie["Username"] != null)
				{
					username = aCookie["Username"];
				}

				if (aCookie["UserID"] != null)
				{
					userID = aCookie["UserID"];
				}
			}

			if (username == "")
			{
				if (txtUsername.Text.Trim() == "")
				{
					username = "Anonymous";
				}
				else
				{
					username = txtUsername.Text.Trim();
				}
			}




			TopicService topicServ = new TopicService();

			string urlTitle = "";

			try
			{
				string ipaddress = Request.ServerVariables["REMOTE_ADDR"].ToString();
				topicID = topicServ.createTopic(txtTitle.Text, message, subforumID, username, userID, ipaddress, out urlTitle);
			}
			catch (Exception ex)
			{
				lblError.Text = ex.Message;
			}

			if (topicID > 0)
			{
				//Response.Redirect("~/index");
				Response.Redirect("~/topic/" + topicID.ToString() + "/1/" + urlTitle);
			}


			//string topicID = HttpContext.Current.Request.QueryString["ID"].ToString();
			//Response.Redirect("~/ViewTopic.aspx?ID=" + topicID + "&Page=0");
		}



		protected void btnPreview_Click(object sender, EventArgs e)
		{
		}
	}


}